import Main from '@/views/main';
import dynamic from 'dva/dynamic';
import {Redirect} from 'dva/router';
import cookie from 'js-cookie';
import ChildrenRouter from './router.children.config';

//登录拦截

//是否登录
const isLogin = () => {
    return cookie.get('userToken') && cookie.get('userToken').split('.').length===3
}

//高阶组件
const isLoginCom = (Com) => {
    if(isLogin()){
        //登录
        return <Com/>
    }else{
        //没有登录
        return <Redirect to="/login"/>
    }   
}

const routes = [  //一级
    {
        path:'/login',
        component:dynamic({
            component:()=> import('@/views/login'),
            // app,  //局部注册model
            // models: () => [
            //     import('@/models/user'),
            // ]
        })
    },
    {
        path:'/main',  //判断是否登录
        component:(props) => isLoginCom(()=><Main {...props}/>),
        routes:[
            ...ChildrenRouter
        ]
    },
    {
        path:'/',
        exact:true,
        component:() => isLoginCom(()=><Redirect to="/main"/>)   //判断是否登录
    },
    {
        path:'*',
        component:()=><div>页面不存在</div>
    }
];

export default routes